package io.xxx.data.sync

import kotlin.reflect.KProperty

interface PayApi {
    fun pay()
}

class AliPay : PayApi {
    override fun pay() {
        println("delegate AliPay")
    }
}

//自己实现的委托
class ScanCodePay : PayApi by AliPay()
//class ScanCodePay : PayApi {
//    private val payApi: PayApi = AliPay()
//    override fun pay() {
//        payApi.pay()
//    }
//}

fun main() {
//    ScanCodePay().pay()
    val bean = Bean()
    bean.name = "Tim"  // setValue set name --> Tim
    bean.name          // getValue get name --> Tim
}

class Bean(){
    var name : String by Delegater()
}

class Delegater {
    var str : String = ""
    operator fun getValue(ref: Any?, p: KProperty<*>) :String {
        println("get ${p.name} --> $str")
        return str
    }
    // ref  进行委托的类的对象 p 属性对象 value 属性的值
    operator fun setValue(ref: Any?, p: KProperty<*>, value: String) {
        str = value
        println("set ${p.name} --> $str")
    }
}